package by.grsu.bso.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import by.grsu.bso.form.CategoryProviderForm;
import by.grsu.bso.model.Category;
import by.grsu.bso.service.ServiceManager;

public class CategoryProviderAction extends Action {
	
	private final static String SUCCESS = "success";
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		HttpSession session = request.getSession(true);
		
		CategoryProviderForm providerForm = (CategoryProviderForm) form;
		List<Category> categoryList = new ArrayList<Category>();
		
		if (providerForm.getParentCategoryId() < 1 ) {
			categoryList = ServiceManager.getCategoryService().findByParent(1);
		} else {
			categoryList = ServiceManager.getCategoryService().findByParent(providerForm.getParentCategoryId());
		}
		
		
		//session.setAttribute("categoryList", categoryList);
		session.setAttribute("categoryList", categoryList);
		
		response.sendRedirect(request.getContextPath());
		return mapping.findForward(SUCCESS);
	}

	

}
